package com.xin.components.base
{
	import flash.events.MouseEvent;

	/**
	 * 按钮基类
	 * @author xin
	 * <p>子类实现goUp(); goOver(); goDown()方法 以改变按钮的状态
	 */	
	public class ButtonBase extends LiteSprite
	{
		protected var _over:Boolean = false;
		protected var _down:Boolean = false;
		protected var _selected:Boolean = false;
		protected var _toggle:Boolean = false;
		
		public function ButtonBase(w:int, h:int, clickHandler:Function = null)
		{
			if (clickHandler != null)
			{
				addEventListener(MouseEvent.CLICK, clickHandler);
			}
			
			super(w, h);
		}
		
		protected override function init():void
		{
			super.init();
			
			mouseChildren = false;
			buttonMode = true;
			useHandCursor = true;
			
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			addEventListener(MouseEvent.ROLL_OVER, onRollOver);
		}
		
		/**
		 * 鼠标弹起
		 */		
		protected function goUp():void{};
		
		/**
		 * 鼠标经过
		 */		
		protected function goOver():void{};
		
		/**
		 * 鼠标按下
		 */		
		protected function goDown():void{};
		
		protected function drawFace():void
		{
			if (_down)
			{
				goDown();
			}
			else if (_over)
			{
				goOver();
			}
			else
			{
				goUp();
			}
		}
		
		/////////////////////////////////////////////
		// 公开方法
		/////////////////////////////////////////////
		public override function draw():void
		{
			drawFace();
		}
		
		/////////////////////////////////////////////
		// 事件处理
		/////////////////////////////////////////////
		protected function onRollOver(e:MouseEvent):void
		{
			_over = true;
			
			if (!_down)
			{
				goOver();
			}
			
			addEventListener(MouseEvent.ROLL_OUT, onRollOut);
		}
		
		protected function onRollOut(e:MouseEvent):void
		{
			_over = false;
			
			if (!_down)
			{
				goUp();
			}
			removeEventListener(MouseEvent.ROLL_OUT, onRollOut);
		}
		
		protected function onMouseDown(e:MouseEvent):void
		{
			_down = true;
			drawFace();
			
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		protected function onMouseUp(e:MouseEvent):void
		{
			if (_toggle && _over)
			{
				_selected = !_selected;
			}
			
			_down = _selected;
			drawFace();
			
			if (stage)
				stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		/////////////////////////////////////////////
		// Getter & Setter
		/////////////////////////////////////////////
		
		/**
		 * 设置选择状态
		 * <p>仅在是toggle按钮下有效
		 */		
		public function set selected(value:Boolean):void
		{
			_selected = value;
			
			//非toggle状态下, 所有选择状态只能为false
			if (!_toggle)
			{
				value = false;
			}
			
			_down = _selected = value;
			drawFace();
		}
		public function get selected():Boolean
		{
			return _selected;
		}
		
		/**
		 * 设置为toggle按钮
		 */		
		public function set toggle(value:Boolean):void
		{
			_toggle = value;
		}
		public function get toggle():Boolean
		{
			return _toggle;
		}
	}
}